package com.wjh.anyview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.anyview.dao.BankQuestionMapper;
import com.wjh.anyview.dao.ExamMapper;
import com.wjh.anyview.dao.QuestionMapper;
import com.wjh.anyview.entity.bo.examServiceBo.NewQuestionBo;
import com.wjh.anyview.entity.dto.CommonResult;
import com.wjh.anyview.entity.po.BankQuestion;
import com.wjh.anyview.entity.po.Exam;
import com.wjh.anyview.entity.po.Question;
import com.wjh.anyview.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjh
 * @date 2021/10/12 22:25
 * @Package com.wjh.anyview.service.impl
 */
@Service
@Slf4j
public class QuestionServiceImpl implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private BankQuestionMapper bankQuestionMapper;

    @Resource
    private ExamMapper examMapper;

    @Override
    public CommonResult<List<Question>> listQuestionByByExamId(Long examId) {
        Exam exam = examMapper.selectById(examId);
        QueryWrapper<BankQuestion> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq("bank_id", exam.getBankId());
        List<BankQuestion> bankQuestionList = bankQuestionMapper.selectList(questionQueryWrapper);
        List<Question> questionList = new ArrayList<>();
        bankQuestionList.forEach(bankQuestion -> {
            Question question = questionMapper.selectById(bankQuestion.getQuestionId());
            //答案置空
            question.setAnswer(null);
            questionList.add(question);
        });

        return CommonResult.operateSuccess(questionList);
    }

    @Override
    public CommonResult<List<Question>> listQuestionByBankId(Long bankId) {
        QueryWrapper<BankQuestion> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq("bank_id", bankId);
        List<BankQuestion> bankQuestionList = bankQuestionMapper.selectList(questionQueryWrapper);
        List<Question> questionList = new ArrayList<>();
        bankQuestionList.forEach(bankQuestion -> {
            Question question = questionMapper.selectById(bankQuestion.getQuestionId());
            questionList.add(question);
        });

        return CommonResult.operateSuccess(questionList);
    }

    @Override
    @Transactional
    public CommonResult<?> addQuestionByBankId(NewQuestionBo newQuestionBo) {
        Question question = new Question();
        question.setQuestion(newQuestionBo.getQuestion());
        question.setAnswer(newQuestionBo.getAnswer());
        //插入新题目
        questionMapper.saveQuestion(question);
        BankQuestion bankQuestion = new BankQuestion();
        bankQuestion.setQuestionId(question.getId());
        bankQuestion.setBankId(newQuestionBo.getBankId());
        //将新题目添加到题库
        bankQuestionMapper.insert(bankQuestion);
        return CommonResult.operateSuccess();
    }
}
